/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 邓大帅
 * Date: 2023-01-27
 * Time: 8:01
 */
public class MyLinkedList {
    // 2、无头双向链表实现
    public class Node {
        int val;
        Node next;
        Node prev;
        public Node(int val) {
            this.val = val;
        }
    }

    Node head;
    Node last;
    int useSize;

    public void CreateLinkedList() {
        Node node1 = new Node(1);
        Node node2 = new Node(1);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.prev = node3;
        node3.prev = node2;
        node2.prev = node1;
        head = node1;
        last = node4;
        useSize = 4;
    }

    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        if(head == null) {
            head = node;
            last = node;
            useSize++;
            return;
        }
        node.next = head;
        head = node;
        useSize++;
    }

    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if(head == null) {
            last = node;
            head = node;
            useSize++;
            return;
        }
        node.prev = last;
        last.next = node;
        last = node;
        useSize++;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        Node node = new Node(data);
        checkOutOfIndex(index);
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == useSize) {
            addLast(data);
            return;
        }
        Node cur = head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
        useSize++;
    }

    private void checkOutOfIndex(int index) throws CheckOutOfIndexException{
        if(index < 0 || index > useSize) {
            throw new CheckOutOfIndexException("位置不合法，请检查");
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        if(head == null) {
            return false;
        }
        Node cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            head.prev = null;
            useSize--;
            return;
        }
        Node cur = head;
        Node curNext = cur.next;
        while(curNext != null) {
            if(cur.val == key) {
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                cur = curNext;
                curNext = cur.next;
                useSize--;
                return;
            }
            cur = curNext;
            curNext = cur.next;
        }
        if(last.val == key) {
            last = last.prev;
            last.next = null;
            useSize--;
            return;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        Node cur = head.next;
        Node curNext = cur.next;
        while(curNext != null) {
            if(cur.val == key) {
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                cur = curNext;
                curNext = cur.next;
                useSize--;
            }
            cur = curNext;
            if(cur != null) {
                curNext = cur.next;
            }
        }
        if(head.val == key) {
            head = head.next;
            head.prev = null;
            useSize--;
        }
        if(last.val == key) {
            last = last.prev;
            last.next = null;
            useSize--;
        }
    }

    //得到单链表的长度
    public int size() {
        return useSize;
    }

    public void display() {
        if(head == null) {
            return;
        }
        Node cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();

    }

    public void clear() {
        Node cur = head;
        Node curNext = cur.next;
        while(curNext != null) {
            cur.next = null;
            cur = curNext;
            curNext = cur.next;
        }
        head = null;
        useSize = 0;
    }
}
